<?php
/**
 * $Id: ModelController.php 639 2008-06-18 10:51:32Z mija $
 * Kontroler modelek
 *
 */
class SetupController extends MainController implements ControllerInterface {
	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		if (  isset($_REQUEST["addVariable"])  ) {
			//Utils::ArrayDisplay($_REQUEST);
			SetupDAL::SaveVariable($_REQUEST["variable"], $_REQUEST["value"], $_REQUEST["description"]);
		}
		$this->smarty->assign('arrayVariables', SetupDAL::GetAllVariables(false) );


		//Utils::ArrayDisplay(SetupDAL::GetAllVariables(false));

	}

	/**
	 * Nowy parametr
	 *
	 */
	public function NewAction() {
		$this->partialTemplate = 'Edit.tpl';
		$this->smarty->assign('objVariable', NULL );
	}


	/**
	 * Edycja parametru
	 *
	 * @param array $param
	 */
	public function EditAction($param) {
		//$this->partialTemplate = 'Edit.tpl';


		$objVariable = SetupDAL::GetVariableByName($param["variable"]);

		$this->smarty->assign('objVariable', $objVariable );
	}

	/**
	 * Czyszczenie cache strony
	 *
	 */
	public function ClearCacheAction() {

			
		$directory = PATH_SITE_CACHE;

		if( !$dirhandle = @opendir($directory) )
		return;

		while( false !== ($filename = readdir($dirhandle)) ) {
			if( $filename != "." && $filename != ".." ) {
				$filename = $directory. "/". $filename;
				if(is_file($filename)) {
					@unlink($filename);
				}
			}
		}
	}

	public function SyncAction() {
		$this->smarty->assign('actionReturn', '');
	}
	
	public function SyncAppCodeAction() {
		$this->partialTemplate = 'Sync.tpl';
		$reply = exec("/opt/WP/bin/default-sync.sh");
		if($reply == 0) {
			$this->smarty->assign('actionReturn', 'Wykonano synchronizacj� kodu aplikacji.');
		} else {
			$this->smarty->assign('actionReturn', 'Nie uda�o si� wykona� synchronizacji. Odpowied� serwera: '.$reply);
		}
		
	}

	/**
	 * Wspolna metoda
	 *
	 */
	public function preDispatch($param) {
		//$this->RunShared('Auth');
		$this->Run();
		//$admin = AuthDAL::GetAdmin();
//		if(!$this->user->CheckAccess('admin')) {
//			die(Header("Location: index".APPLICATION_FILE_TYPE));
//		}
	}
	public function postDispatch($param) {
		
	}
}
